package app.netant.book.base;

import java.io.File;
import java.net.URLEncoder;

import app.client.file.downs.FileDown;
/**
 * 久久 - 302 + 乱码处理
 * @author JiaJ
 *
 */
public abstract class BaseDown extends Thread {
	
	private String isDown = "0";
	
	private String filePath;
	private String url;
	
	private String refer = "";
	
	private String charset;
	
	private FileDown down;
	
	public void setFilePath(String filePath) {
		this.filePath = filePath;
	}

	public String isDown() {
		return isDown;
	}
	
	public void setUrl(String url) {
		this.url = url;
	}

	public void setDown(String isDown) {
		this.isDown = isDown;
	}
	
	public void setRefer(String refer){
		this.refer = refer;
	}
	
	public void setCharset(String charset){
		this.charset = charset;
	}

	@Override
	public void run() {
		
		try {
			
			File file = new File(this.filePath);
			if(file.exists()){
				file.delete();
			}
			
			while(true){
				this.downFile(filePath, url);
				this.isDown = "1";
				break;
			}
			
		} catch (Exception e) {
			
			this.isDown = "2";
			
			File file = new File(this.filePath);
			if(file.exists()){
				file.delete();
			}
			
			e.printStackTrace();
			
		}
		
	}
	
	public abstract void downFile(String filePath,String url) throws Exception;
	
	public void downOwnFile(String filePath,String url) throws Exception{
		this.down = new FileDown();
		
		if(this.charset != null && !"".equals(this.charset)){
			down.setCharset(this.charset);
		}
		
		down.downImage(url, filePath);	
	}
	
	public void downReferFile(String filePath,String url) throws Exception{
		this.down = new FileDown();
		
		if(this.charset != null && !"".equals(this.charset)){
			down.setCharset(this.charset);
		}
		
		down.downRefer(url, filePath,this.refer);	
	}
	
	public void downUnicodeFile(String filePath,String url) throws Exception{
		
		String urlName = url.substring(url.lastIndexOf("/")+1);
		urlName = URLEncoder.encode(urlName, "utf-8");
		String urlPlus = url.substring(0,url.lastIndexOf("/"));
		
		System.out.println("下载文件路径："+filePath);
		System.out.println("下载链接地址："+ urlPlus + "/"+ urlName);
		
		this.downOwnFile(filePath, urlPlus + "/"+ urlName);
		
	}
	
	public void downLocationFile(String filePath,String url) throws Exception {
		this.down = new FileDown();
		
		if(this.charset != null && !"".equals(this.charset)){
			down.setCharset(this.charset);
		}
		
		down.downLocation(url, filePath);	
	}
	
	public String getLocation(String filePath,String url) throws Exception{
		this.down = new FileDown();
		
		if(this.charset != null && !"".equals(this.charset)){
			down.setCharset(this.charset);
		}
		
		return this.down.getLocation(url, filePath);
	}
	
	public void downLocationUnicodeFile(String filePath,String url) throws Exception{
		String location = this.getLocation(filePath,url);		
		this.downUnicodeFile(filePath, location);
	}
	
	public void downLocationUnicodeFilePlus(String filePath,String url,String preHref) throws Exception{
		String location = this.getLocation(filePath, url);	
		if(location.startsWith("/")){
			location = preHref + location;
		}
		this.downUnicodeFile(filePath, location);
	}
	
	/**
	 * 强制关闭
	 */
	public void stopThread(){
		if(this.down != null){
			this.down.stop();
		}
	}
	
}
